<%@ include file="shared_header.jsp" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="vrp.SOrder" %>
<%@ page import="vrp.SOrderItem" %>
<%@ page import="vrp.Product" %>

<%
	SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
	PersistenceManager pm = PMF.get().getPersistenceManager();
	String query = "";
%>

<%
    UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();
    if (user != null) {
%>
		<p>Hello, <%= user.getNickname() %>! (You can
		<a href="<%= userService.createLogoutURL(request.getRequestURI()) %>">sign out</a>.)</p>
<%
    } else {
%>
		<p>Hello!
		<a href="<%= userService.createLoginURL(request.getRequestURI()) %>">Sign in</a>
		to include your name with greetings you post.</p>
<%
    }
%>

		<h2>List of Sale Order Items</h2>
		<table>
<%
    String queryProducts = "select from " + Product.class.getName();
    List<Product> products = (List<Product>) pm.newQuery(queryProducts).execute();
    
    
    long sorderID = Integer.parseInt(request.getParameter("sorder"));
    Key sorderKey = KeyFactory.createKey(SOrder.class.getSimpleName(), sorderID);
    SOrder sorder = pm.getObjectById(SOrder.class, sorderKey);
    LinkedList<SOrderItem> sorderitems = sorder.getSOrderItems();
    if (sorderitems.isEmpty()) {
%>
			<tr><td>There is no sale order item for this order.</td></tr>
<%
    } else {
%>
			<tr>
				<th>Product</th>
				<th>Quantity</th>
				<th>Delivery Date</th>
				<th>Status</th>
				<th>Action <%= sorderitems.size() %></th>
			</tr>
<%
        for (SOrderItem oi : sorderitems) {
%>
			<tr>
				<td><%= (pm.getObjectById(Product.class, oi.getProduct())).getName() %></td>
				<td><%= oi.getQuantity() %></td>
				<td><%= sdf.format(oi.getDeliveryDate()) %></td>
				<td><%= oi.getSOrderItemStatus() %></td>
				<td>
					<input type="button" value="Edit" class="buttonedit"/>
					<input type="button" value="Delete" class="buttondelete"/>
				</td>
			</tr>
			<tr style="display: none;">
				<td colspan="5">
					<form action="/editsorderitem" method="post">
						<div><b>Edit Sale Order Item</b></div>
			    		<div>Product:&nbsp;
			    			<select name="product">
<%
        					for (Product p : products) {
%>			    			
							 	<option value="<%= p.getKey().getId() %>" <%= (p.getKey().getId()==oi.getProduct().getId())?"selected":"" %>><%= p.getName() %></option>
<%
        					}
%>							</select>
			    		</div>
			    		<div>Quantity:&nbsp;<input type="text" name="quantity" value="<%= oi.getQuantity() %>" /></div>
			    		<div>Delivery Date:&nbsp;<input type="text" name="deliveryDate" value="<%= sdf.format(oi.getDeliveryDate()) %>" /></div>
			    		<div>Sale Order Item Status:&nbsp;<input type="text" name="sorderItemStatus" value="<%= oi.getSOrderItemStatus() %>" /></div>
						<input type="hidden" name="sorder" value="<%= sorderID %>"/>
						<input type="hidden" name="sorderItem" value="<%= oi.getKey().getId() %>"/>
						<input type="submit" value="Edit" />
						<input type="button" value="Cancel" class="buttoncancel" />
					</form>
				</td>
			</tr>
			<tr style="display: none;">
				<td colspan="5">
					<form action="/deletesorderitem" method="post">
						<div><b>Delete Sale Order Item</b></div>
			    		<div>This action would delete the above sale order item permanently. Proceed?</div>
						<input type="hidden" name="sorder" value="<%= sorderID %>"/>
						<input type="hidden" name="sorderItem" value="<%= oi.getKey().getId() %>"/>
						<input type="submit" value="Delete" />
						<input type="button" value="Cancel" class="buttoncancel" />
					</form>
				</td>
			</tr>
<%
        }
    }
%>
			
		</table>
		<input type="button" value="Add SOrder" class="buttonadd" />
  		<div style="display: none;">
			<form action="/addsorderitem" method="post">
	    		<div>Customer:&nbsp;
	    			<select name="product">
<%
     				for (Product p : products) {
%>			    			
					 	<option value="<%= p.getKey().getId() %>"><%= p.getName() %></option>
<%
        			}
%>							
					</select>
	    		</div>
	    		<div>Quantity:&nbsp;<input type="text" name="quantity" /></div>
	    		<div>Delivery Date:&nbsp;<input type="text" name="deliveryDate" /></div>
	    		<div>
	    			<input type="hidden" name="sorder" value="<%= sorderID %>" >
	    			<input type="submit" value="Add" />
	    			<input type="button" value="Cancel" class="buttoncancel" />
	    		</div>
	  		</form>
	  	</div>

<%
    pm.close();
%>

<%@ include file="shared_footer.jsp" %>